using System;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Security;


namespace newtelligence.DasBlog.Web.Services 
{
	
	/// <summary>
	/// Currently only used to approve comments, but can easily be modified 
	/// to approve other content types (entries, trackbacks etc.).
	/// </summary>
    public partial class WebWebServices
	{
		
		[OperationContract, WebGet(UriTemplate="/approveItem?entryId={entryId}&commentId={commentId}&referralPermalink={referralPermalink}&type={type}")]
		public void ApproveItemHandler(
            string entryId,
			string commentId,
			string referralPermalink,
			string type) 
		{

			SiteConfig siteConfig = SiteConfig.GetSiteConfig();

            if (!(Roles.IsUserInRole("admin")||Roles.IsUserInRole("contributor")))
            {
                WebOperationContext.Current.OutgoingResponse.Location = Utils.GetStartPageUrl(siteConfig);
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
                return;
            }

			
			string redirectUrl = Utils.GetStartPageUrl();

			// make sure the entry param is there
			if (entryId == null || entryId.Length == 0) {
                WebOperationContext.Current.OutgoingResponse.Location = Utils.GetStartPageUrl(siteConfig);
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
                WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
                return;
			}
			else {
				try {

					ILoggingDataService loggingService = LoggingDataServiceFactory.GetService(SiteConfig.GetLogPathFromCurrentContext());
                    IBlogDataService dataService = BlogDataServiceFactory.GetService(SiteConfig.GetContentPathFromCurrentContext(), System.Web.Hosting.HostingEnvironment.MapPath(SiteConfig.GetSiteConfig().BinariesDir), loggingService);
			
					Entry entry = dataService.GetEntry( entryId );
					if ( entry != null ) {
						if (commentId != null && commentId.Length > 0) {

							ISpamBlockingService spamBlockingService = siteConfig.SpamBlockingService;
							if (spamBlockingService != null) 
							{
								Comment comment = dataService.GetCommentById(entryId, commentId);
								if ( (comment != null) && (comment.SpamState == SpamState.Spam) )
								{
									try 
									{
										spamBlockingService.ReportNotSpam(comment);
									}
									catch(Exception ex)
									{
										loggingService.AddEvent(new EventDataItem(EventCodes.Error, String.Format("Unable to report comment {0} incorrectly marked as spam. Original exception: {1}", comment.EntryId, ex), Utils.GetPermaLinkUrl(entryId)));
									}
								}
							}

							dataService.ApproveComment(entryId, commentId);

							loggingService.AddEvent(
								new EventDataItem( 
								EventCodes.CommentApproved, commentId, 
								Utils.GetPermaLinkUrl(entryId)));
	
							redirectUrl = Utils.GetCommentViewUrl(entryId);
						}
					}
				}
				catch( Exception exc ) {
					// absorb
					ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error,exc);
				}
			}

            WebOperationContext.Current.OutgoingResponse.Location = redirectUrl;
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
            WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
		}
	}
}
